<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>脚本编辑</title>
    <script src="${ctx}/extends/jquery.min.js" type="text/javascript"></script>
    <#include "/pubHeadStyle.html">
    <script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/codemirror.js"></script>
    <script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/InitMirror.js"></script>
    <script type="text/javascript" src="${ctx}/workflowStatic/js/commonScript.js"></script>
</head>
<body>
<form class="layui-form" action="" style="padding: 0 10px" lay-filter="scriptEditForm">
    <table class="layui-table">
        <tr>
            <td>名称:<span style="color:red">*</span></td>
            <td>
                <div >
                    <input type="hidden" name="id"  autocomplete="off" class="layui-input" value="${id}">
                    <input type="text" name="name"  autocomplete="off" class="layui-input" value="${sysScript.name}" lay-verify="required">
                </div>
            </td>
        </tr>
        <tr>
            <td>脚本:</td>
            <td><textarea  name="script" codemirror="true" class="layui-textarea">${sysScript.script}</textarea></td>
        </tr>
        <tr>
            <td>脚本分类:<span style="color:red">*</span></td>
            <td>
                <div class="layui-input-inline">
                    <input type="text" name="category" id="category"  autocomplete="off" class="layui-input" lay-verify="required" value="${sysScript.category}">
                    <select name="catName" id="catName" class="filter-value" lay-filter="category">
                        <option value="">------全部------</option>
                        <#list categoryList as list>
                        <option value="${list}">${list}</option>
                    </#list>
                    </select>
                </div>
            </td>
        </tr>
        <tr>
            <td>备注:</td>
            <td><textarea  name="memo"  class="layui-textarea">${sysScript.memo}</textarea></td>
        </tr>
    </table>

    <div class="layui-btn-container layui-footer" style="position: absolute;left:37%;bottom:10px">
        <button type="button"  lay-submit class="layui-btn" lay-filter="dataFormSave">确定</button>
        <button class="layui-btn layui-btn-danger" id="close">取消</button>
        <button type="button" class="layui-btn commonScript" id="scriptEdit">脚本编辑器</button>
    </div>
</form>
<script>
    layui.use(['index','form','setter','commons','layer','element'],function(){
        var form = layui.form,
            setter = layui.setter,
            commons = layui.commons,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;
        var id='${id}';
        form.render();
        //category获取下拉框的数据
        form.on('select(category)', function(data){
            var value= data.value;
            if(value!=""){
                var text = $(data.elem).find("option[value="+value+"]").text();
                $('#category').val(text)
            }
        });
        //监听提交
        form.on('submit(dataFormSave)', function (data) {
            $.ajax({
                url: serverPath.formPath + setter.formLinkPath + "script/checkRepeatName",
                type: 'post',
                data: {name:data.field.name,id:data.field.id},
                dataType: 'json',
                success: function (result) {
                    if (result.code==0) {
                        //名称可以使用，开始执行   保存功能
                        InitMirror.editor.save();
                        data.field.script = $('textarea[name="script"]').val();
                        $.ajax({
                            url: serverPath.formPath + setter.formLinkPath + "script/save",
                            type: "POST",
                            data: data.field,
                            dataType:'json',
                            success: function (result) {
                                if (commons._filterRecord(result)) {
                                    layer.alert(result.msg, function (index) {
                                        layer.close(index);
                                        parent.layui.table.reload('scriptItem', {
                                            page: {curr: 1}
                                        });
                                        parent.layui.totalFilter.init('scriptItem');
                                        parent.layer.close(index1);
                                    });
                                } else {
                                    layer.alert(result.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert("保存失败！" + XMLHttpRequest.status);
                            }
                        });
                    }else{
                        //名称已存在
                        parent.layer.msg(result.msg, {time: 2500, icon: 5});
                    }
                }
            });
        });

        var index1 = parent.layer.getFrameIndex(window.name);
        $('#close').click(function () {
            parent.layer.close(index1);
        })
    })
</script>
</body>
</html>